<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 列表管理
 */
namespace app\product\model;

use  app\product\model\Model;
use  Mscore\Core\Redis;
use  Mscore\Core\Lib\Upload;

class Tradecontrol extends Model{
	
    private $operate = 'pro_traderisk';
    private $slide = 'pro_tradeslide';
    private $hashfix;

    /**
     * 初始化自动连接方法
     * @param array $__construct 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->hashfix=config('redis.redis_db.db_hashfix');
    }

    /**
     * 列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //矿机列表
        $list['datalist']= Model::name($this->operate)
            ->alias('a')
            ->field('a.*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.aid asc, a.id desc')
            ->select();
        //数据总数
        $list['total']=Model::name($this->operate)
            ->alias('a')
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * @param array $info 
     * @param array $where 
     */ 
    public function info($id=null,$field='*'){  
        $info=Model::name($this->operate)
           ->field($field)
           ->where(['id'=>$id])
           ->find();
        if(isset($info['point'])){
            $info['point']=json_decode($info['point'],true);  
        }
        return  $info;
    }

    /**
     * 保存
     * @param array $save 
     * @param array $where 
     */ 
    public function save($data=[]){

        $data  = [
            'id'=>$data['id'],
            'aid'=>$data['aid'],
            'username'=>$data['aid']>0?$data['username']:'platform(平台)',
            'title'=>$data['title'],
            'point'=>json_encode($data['point'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'proid'=>$data['proid'],
            'procode'=>$data['procode'],
            'status'=>$data['status'],
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        switch (true) {
            case isset($data['id'])&&!empty($data['id']):
                Model::name($this->operate)->where('id=' . $data['id'])->update($data);
                return  $data['id'];
                break;
            
            default:
                unset($data['id']);
                $id = Model::name($this->operate)->insertGetId($data);
                return $id;
                break;
        }
        return false;
    }

    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */
    public function del($id=null){
     //删除
        if ($id) {
            Model::name($this->operate)->where('id='.$id)->delete();
            return true;
        }
        return false;
    }

    /**
     * 保存
     * @param array $savedisk 
     * @param array $where 
     */ 
    public function savedisk($data=[]){
        switch (true) {
            case !empty($data):
                if(isset($data['id'])&&!empty($data['id'])){
                    $id=$data['id'];
                    $data['starttime']=strtotime($data['starttime']);
                    $data['timelong']=$data['runtime']>0?$data['runtime']:$data['timelong'];
                    $expire=$data['auto']==1?($data['starttime']-time()+$data['timelong']):$data['timelong'];
                    $auto=$data['auto'];
                }else{
                    return false;
                }
                $hashfix = config('redis.redis_db.db_hashfix');
                $zero = config('tradecontrol');
                $data  = [
                            'aid'=>$data['aid'],
                            'proid'=>$data['proid'],
                            'procode'=>$data['procode'],
                            'point'=>$data['point'],
                            'timelong'=>$data['timelong'],
                            'starttime'=>$data['auto']==1?$data['starttime']:time(),
                        ];
                        Redis::exec('hset',[$hashfix.$zero['key'],$data['procode'].$data['aid'],json_encode($data)]);
                        $data['auto']=$auto;
                        $data['point']=json_encode($data['point'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
                        Model::name($this->operate)->where('id=' . $id)->update($data);
                        return  $id;
                return true;
                break;
            default:
                break;
        }
        return false;
    }

    /**
     * 用户信息
     * @param array $userinfo 
     * @param array $where 
     */ 
    public function userinfo($where=null){
        return Model::name($this->slide)->where($where)->find();
    }

    /**
     * 保存
     * @param array $saveuser 
     * @param array $where 
     */ 
    public function saveuser($data=[]){
        $status=Model::name($this->slide)->where('id=1')->find();
        if(!empty($status)){
            Model::name($this->slide)->where('id=1')->update($data);
        }else{
            Model::name($this->slide)->insert($data);
        }
        
        return true;
    }

    /**
     * 更新运营
     * @param array $disk 
     * @param array $where 
     */ 
    public function operatesave($where='',$data=[]){
       if ($where) {
            Model::name($this->operate)->where($where)->update($data);
            return true;
        }
        return false;
    }

    /**
     * 操作redis
     * @param array $writeRedis
     * @param array $where $page $num
     */
    public function execRedis($meth,$arr,$db='0')
    {
        if($meth){
            $arr[0]=$this->hashfix.$arr[0];
            $data = Redis::exec($meth,$arr,$db);
            return $data;
        }        
        return false;
    }

    /**
     * 更新风控
     * @param array $updateconf
     * @param array $where $page $num
     */
    public function updateconf($data='')
    {
        if(isset($data['id'])&&!empty($data['id'])){
            Model::name($this->operate)->where('id=' . $data['id'])->update($data);
            return  true;
        }
        return  false;
    }

}
